import React, { forwardRef, useImperativeHandle, useState } from 'react';

import {
    Button,
    DatePicker,
    Form,
    Input,
    Modal,
    Radio,
    Tag,
    Select,
    InputNumber,
    message,
    Row,
    Col,
} from 'antd';
import { modalLayoutLg, modalLayoutSm } from '@/config/layout';
import { request } from 'umi';
const DictModal = (props: any, refs: any) => {
    const [form] = Form.useForm();
    // const [fileList,setfileList] = useState([]);
    const [measureClassData, setmeasureClassData] = useState([]);
    const { setFieldsValue, resetFields } = form;
    const [code, setCode] = useState('');
    const [isModalVisible, setIsModalVisible] = useState(false);
    const { TextArea } = Input;
    //显示弹窗
    const showModal = () => {
        setIsModalVisible(true);
    };

    //取消
    const handleCancel = () => {
        setIsModalVisible(false);
        setCode('');
        resetFields();
        // props.callback && props.callback();
    };
    //确定
    const handleOk = () => {
        form
            .validateFields()
            .then((vals) => {
                let Obj = form.getFieldsValue();

                Obj = {
                    ...Obj,
                };
                if (Obj.newPassword != Obj.confirmPassword) {
                    message.error('两次输入的新密码不一致');
                    return;
                }

                let reg = /^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?!([^(0-9a-zA-Z)])+$)(?![0-9a-z]+$)(?![0-9A-Z]+$)(?![0-9a-zA-Z]+$)^.{8,}$/;
                if (!reg.test(Obj.newPassword)) {
                    message.error('密码必须是包含大小写字母、数字、特殊符号的8位以上组合');
                    return;
                }
                const url = '/sys/account/modifyPwd?oldPassword=' + Obj.oldPassword + '&newPassword=' + Obj.newPassword;
                debugger
                request(url, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    data: Obj,
                }).then((res) => {
                    message.success('密码修改成功');
                    setIsModalVisible(false);
                    handleCancel();
                    props.callback && props.callback();
                });
            })
            .catch((info) => { });
    };

    useImperativeHandle(refs, () => ({
        init: () => {
            resetFields();
            showModal();
            // setfileList([]);
        },
    }));

    return (
        <>
            <Modal
                title={'修改密码'}
                visible={isModalVisible}
                onOk={handleOk}
                onCancel={handleCancel}
                centered
                width={800}
                maskClosable={false}
                bodyStyle={{
                    maxHeight: window.innerHeight - 150,
                    overflowY: 'auto',
                }}
            >
                <Form form={form} layout="vertical" {...modalLayoutSm}>
                    <Form.Item
                        label="旧密码"
                        name="oldPassword"
                        rules={[{ required: true, message: '请输入旧密码!' }]}
                    >
                        <Input />
                    </Form.Item>
                    <Form.Item
                        label="新密码"
                        name="newPassword"
                        rules={[{ required: true, message: '请输入新密码' }]}
                    >
                        <Input />
                    </Form.Item>
                    <Form.Item
                        label="确认新密码"
                        name="confirmPassword"
                        rules={[{ required: true, message: '请确认新密码' }]}
                    >
                        <Input />
                    </Form.Item>
                </Form>
            </Modal>
        </>
    );
};
const AddMCom = forwardRef(DictModal);

export default AddMCom;
